#include "MainMenu.h"
#include <iostream>

MainMenu::MainMenu(Artist* artistPointer, StateMachine* statePointer)
{
    std::cout << "oh hi guys\n";
    stateMachine = statePointer;
    artist = artistPointer;

    save.initialize(artist, "system/Save.png");
    newGame.initialize(artist, "system/NewGame.png");
    continueGame.initialize(artist, "system/Continue.png");
    loadGame.initialize(artist, "system/LoadGame.png");
    editor.initialize(artist, "system/Editor.png");
    quit.initialize(artist, "system/Quit.png");
    selector.initialize(artist, "system/Selector.png");

    menuState=NEW_GAME;
}

void MainMenu::setState(State newstate)
{
    menuState=newstate;
}

void MainMenu::display()
{
    SDL_Surface* screen = artist -> getScreen();

    artist -> clear();

    if(numOptions>5)
        save.display(screen);

    if(numOptions>4)
        continueGame.display(screen);

    selector.display(screen);
    loadGame.display(screen);
    newGame.display(screen);
    editor.display(screen);
    quit.display(screen);
}

void MainMenu::setPositions()
{
    numOptions=4;

    selector.set_x(200);
    continueGame.set_x(300);
    newGame.set_x(300);
    loadGame.set_x(300);
    editor.set_x(300);
    save.set_x(300);
    quit.set_x(300);

    if( menuState == SAVE)
    {
        numOptions=6;
        menuState = CONTINUE;
    }
    else if (menuState == IN_GAME)
    {
        numOptions = 5;
        menuState = CONTINUE;
    }

    start = (WINDOW_HEIGHT - 300 - 300/numOptions) / 2;

    continueGame.set_y(start + 300/numOptions);
    save.set_y(start + 300*2/numOptions);

    place = 1;

    if(numOptions==5)
    {
        place=2;
    }
    else if(numOptions==6)
    {
        place=3;
    }

    newGame.set_y(start + 300*place/numOptions);
    loadGame.set_y(start + 300*(place+1)/numOptions);
    editor.set_y(start + 300*(place+2)/numOptions);
    quit.set_y(start + 300*(place+3)/numOptions);

}

void MainMenu::update()
{
    switch(menuState)
    {
        case CONTINUE: selector.set_y(start + 300/numOptions); break;
        case SAVE:     selector.set_y(start + 300*2/numOptions); break;
        case NEW_GAME: selector.set_y(start + 300*(place)/numOptions); break;
        case LOAD_GAME: selector.set_y(start + 300*(place+1)/numOptions); break;
        case EDITOR: selector.set_y(start + 300*(place+2)/numOptions); break;
        case QUIT: selector.set_y(start + 300*(place+3)/numOptions); break;
        default: menuState = NEW_GAME;
                selector.set_y(start + 300*(place+1)/numOptions);
    }

    SDL_Rect box = selector.get_bounds();
    int target;
    double ratio;
    int diff = (300*(place+3)/numOptions) - (300/numOptions);

    Image* images[] = {&continueGame, &save, &newGame, &loadGame, &editor, &quit};

    for(int i=0; i<6; i++)
    {
        ratio = (double)abs(box.y - images[i]->get_bounds().y)/(double)diff;
        target = box.x + box.w + 40 + 2*ratio*(280 - box.x - box.w);
        images[i]->set_x(images[i]->get_bounds().x + (target-images[i]->get_bounds().x)/12);
    }
}

void MainMenu::run()
{
    setPositions();
    artist->hideCursor();

    while( stateMachine -> getState() == MAIN_MENU )
    {

        check_events();

        update();

        display();

        artist -> flip();

        artist -> framerate_limit(true);
    }
}

void MainMenu::cycle(std::string dir)
{
    if(dir == "UP")
    {
        switch(menuState)
        {
            case CONTINUE: menuState = QUIT; break;
            case SAVE:     menuState = CONTINUE; break;
            case NEW_GAME: if(numOptions==6)
                              menuState = SAVE;
                           else if(numOptions == 5)
                              menuState = CONTINUE;
                           else
                              menuState = QUIT;
                           break;
            case LOAD_GAME: menuState = NEW_GAME; break; break;
            case EDITOR: menuState = LOAD_GAME; break; break;
            case QUIT: menuState = EDITOR; break; break;
            default: menuState = NEW_GAME;
        }
    }
    else if(dir == "DOWN")
    {
        switch(menuState)
        {
            case CONTINUE:  if(numOptions==6)
                               menuState = SAVE;
                            else
                               menuState = NEW_GAME;
                            break;
            case SAVE:      menuState = NEW_GAME; break;
            case NEW_GAME:  menuState = LOAD_GAME; break;
            case LOAD_GAME: menuState = EDITOR; break; break;
            case EDITOR:    menuState = QUIT; break;
            case QUIT:      if(numOptions>4)
                              menuState = CONTINUE;
                            else
                              menuState = NEW_GAME;
                            break;
            default: menuState = NEW_GAME;
        }
    }
    else if(dir == "ESC")
    {
        if(menuState==QUIT)
            stateMachine -> setState(QUIT);
        else
            menuState=QUIT;
    }
    else if (dir == "ENTER")
    {
        stateMachine -> setState(menuState);
    }
}

void MainMenu::check_events(){
    while( SDL_PollEvent( &event ) )
    {
        if( event.type == SDL_QUIT )
        {
            stateMachine -> setState(QUIT);
            return;
        }
        else
        {
            Uint8 *keystate = SDL_GetKeyState(NULL);
            if(keystate[SDLK_DOWN] && !keystate[SDLK_UP])
                cycle("DOWN");
            else if(keystate[SDLK_UP] && !keystate[SDLK_DOWN])
                cycle("UP");
            else if(keystate[SDLK_q] || keystate[SDLK_x] || keystate[SDLK_ESCAPE])
                cycle("ESC");
            else if(keystate[SDLK_z] || keystate[SDLK_RETURN])
                cycle("ENTER");
        }
    }
}
